package the.workshop.bu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import the.broccoli.auth.UserContext;
import the.broccoli.uc.entity.User;
import the.broccoli.uc.service.IUserService;
import the.broccoli.uc.vo.UserCondition;
import the.workshop.bu.dto.StaSalAmountDTO;
import the.workshop.bu.dto.StaSalCreditDTO;
import the.workshop.bu.dto.StaSalCustomerDTO;
import the.workshop.bu.mapper.StatisticMapper;
import the.workshop.bu.service.IStatisticService;
import the.workshop.bu.vo.StatisticProductVO;
import the.workshop.bu.vo.StatisticSalesmanVO;

import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticServiceImpl implements IStatisticService {

    @Autowired
    private StatisticMapper statisticMapper;

    @Autowired
    private IUserService userService;

    @Override
    public List<StatisticSalesmanVO> salesman() {
        List<StatisticSalesmanVO> dataList = new ArrayList<>();

        List<User> userList = userService.listByUserType("1");
        for (User user : userList) {
            StatisticSalesmanVO vo = new StatisticSalesmanVO();
            vo.setSalesmanId(user.getUserId());
            vo.setSalesmanName(user.getUserName());
            dataList.add(vo);
        }

        List<StaSalCustomerDTO> customerList = statisticMapper.selectListForStaSalCustomer();
        if (!CollectionUtils.isEmpty(customerList)) {
            for (StatisticSalesmanVO data : dataList) {
                for (StaSalCustomerDTO dto : customerList) {
                    if (data.getSalesmanId().equals(dto.getSalesId())) {
                        data.setCustomerCount(dto.getCustomerCount());
                        data.setPotentialCount(dto.getPotentialCount());
                        break;
                    }
                }
            }
        }

        List<StaSalCreditDTO> creditList = statisticMapper.selectListForStaSalCredit();
        if (!CollectionUtils.isEmpty(creditList)) {
            for (StatisticSalesmanVO data : dataList) {
                for (StaSalCreditDTO dto : creditList) {
                    if (data.getSalesmanId().equals(dto.getSalesId())) {
                        data.setCreditingCount(dto.getCreditingCount());
                        data.setCreditedCount(dto.getCreditedCount());
                        break;
                    }
                }
            }
        }

        List<StaSalAmountDTO> amountList = statisticMapper.selectListForStaSalAmount();
        if (!CollectionUtils.isEmpty(amountList)) {
            for (StatisticSalesmanVO data : dataList) {
                for (StaSalAmountDTO dto : amountList) {
                    if (data.getSalesmanId().equals(dto.getSalesId())) {
                        data.setAmount(dto.getAmount());
                        break;
                    }
                }
            }
        }


        return dataList;
    }

    @Override
    public List<StatisticProductVO> product() {
        return null;
    }
}
